package com.hn.shopadmin.controller;

import com.github.pagehelper.PageInfo;
import com.hn.kiss.modules.sys.shopcommon.result.CommonPage;
import com.hn.kiss.modules.sys.shopdb.entity.Menu;
import com.hn.shopadmin.dto.MenuDTO;
import com.hn.shopadmin.service.MenuService;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

@Api(tags = "MenuController", description = "菜单管理业务接口")
@RestController
@RequestMapping("/menu")
@Validated
public class MenuController {
    @Autowired
    MenuService menuService;


    @ApiOperation("一级菜单添加")
    @PostMapping("/addParent")
    public CommonResult addParent(Menu menu){
        int count = menuService.addParent(menu);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("子菜单添加")
    @PostMapping("/addChild")
    public CommonResult addChild(Menu menu){
        int count = menuService.addChild(menu);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("菜单信息修改")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    @PutMapping("/update/{id}")
    public CommonResult update(@PathVariable("id")String id, Menu menu){
        menu.setId(id);
        int count = menuService.update(menu);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("菜单删除")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query")
    @DeleteMapping("/del")
    public CommonResult del(@NotBlank(message = "id不能为空")String id){
        int count = menuService.del(id);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("一级菜单分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "菜单名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/listPageParent")
    public CommonPage<Menu> listPageParent(@NotNull(message = "page不能为空") Integer page,
                                           @NotNull(message = "limit不能为空") Integer limit,
                                           String keywork){
        PageInfo<Menu> menuPageInfo = menuService.listPageParent(page, limit, keywork);
        return CommonPage.restPage(menuPageInfo);
    }

    @ApiOperation("二级菜单分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "keywork", value = "菜单名称", dataType = "String", paramType = "query")
    })
    @GetMapping("/listPageHtml")
    public CommonPage<MenuDTO> listPageHtml(@NotNull(message = "page不能为空") Integer page,
                                           @NotNull(message = "limit不能为空") Integer limit,
                                           String parentId,
                                           String keywork){
        PageInfo<MenuDTO> menuDTOPageInfo = menuService.listPageHtml(page, limit, parentId, keywork);
        return CommonPage.restPage(menuDTOPageInfo);
    }

    @ApiOperation("按钮列表")
    @ApiImplicitParam(name = "parentId", value = "页面id", required = true, dataType = "String", paramType = "query")
    @GetMapping("/listButton")
    public CommonResult<List<MenuDTO>> listButton(@NotBlank(message = "parentId不能为空") String parentId){
        List<MenuDTO> menuDTOS = menuService.listButton(parentId);
        return CommonResult.success(menuDTOS);

    }



}
